<?php /*a:1:{s:59:"C:\wamp64\www\tp\app\admin\view\zhusu\reportforms\index.php";i:1697162157;}*/ ?>
<style>
#organizationTreeBar{padding:10px 15px;border:1px solid #e6e6e6;background-color:#f2f2f2}
#organizationTree{border:1px solid #e6e6e6;border-top:none;padding:10px 5px;overflow:auto;height:-webkit-calc(100vh - 260px);height:-moz-calc(100vh - 260px);height:calc(100vh - 260px)}
.layui-tree-entry .layui-tree-txt{padding:0 5px;border:1px transparent solid;text-decoration:none!important}
.layui-tree-entry.organ-tree-click .layui-tree-txt{background-color:#fff3e0;border:1px #ffe6b0 solid}
</style>
<div class="layui-fluid">   
    <div class="layui-card">
        <div class="layui-card-header">
            <div class="layui-btn-group">
                <button class="layui-btn" id="top-zhusu-add"><i class="layui-icon layui-icon-add-circle"></i> 添加</button>
                <button class="layui-btn" id="top-zhusu-del"><i class="layui-icon layui-icon-delete"></i> 删除</button>
            </div>
        </div>
        <div class="layui-card-body">
            <table lay-filter="zhusu" id="zhusu"></table>
        </div>
    </div>
</div>
<!--JS部分-->
<script type="text/javascript">
layui.use(['zTree','buildItems', 'xmSelect', ], function(){
    var app_root = layui.cache.maps +'zhusu.reportforms/';
    var layer=layui.layer,table=layui.table,form=layui.form,admin=layui.admin;
    /*数据表渲染*/
   
    table.render({
        elem: '#zhusu',
        cols: [[
            {type:'checkbox',fixed:'left'},
            {field:'id',width:60,unresize:true,align:'center',title:'ID'},
            {field:'class',edit:'text',width:55,align:'center',title:'班次'},
            {field:'order_id',edit:'text',width:95,align:'center',title:'订单号'},
            {field:'product_id',edit:'text',width:200,align:'center',title:'产品名称'},
            {field:'number',edit:'text',width:75,align:'center',title:'机台编号'},
            {field:'unit',edit:'text',width:55,align:'center',title:'单位'},
            {field:'material_id',edit:'text',width:80,align:'center',title:'用料'},
            {field:'colour_id',edit:'text',width:80,align:'center',title:'颜色/色粉'},
            {field:'order_number',edit:'text',width:80,align:'center',title:'订单数量'},
            {field:'qualified_quantity',edit:'text',width:100,align:'center',title:'生产合格数量'},
            {field:'working_hours',edit:'text',width:100,align:'center',title:'实际生产工时'},
            {field:'scrapped_part',edit:'text',width:80,align:'center',title:'残品数量'},
            {field:'operator',edit:'text',width:80,align:'center',title:'操作工'},
            {field:'accumulate_number',edit:'text',width:100,align:'center',title:'合格累计数量'},
            {field:'debugging_time',edit:'text',width:100,align:'center',title:'换模调试时间'},
            {field:'change_material_time',edit:'text',width:100,align:'center',title:'换料烘料时间'},
            {field:'weight',edit:'text',width:80,align:'center',title:'产品单重'},
            {field:'abnormal_time',edit:'text',width:180,align:'center',title:'其他异常事件'},
            {field:'remarks',edit:'text',width:180,align:'center',title:'备注'},

            {fixed:'right',width:100,align:'center',toolbar:'<div><a class="layui-btn layui-btn-xs" lay-event="edit">编辑</a><a class="layui-btn layui-btn-xs layui-btn-danger" lay-event="del">删除</a></div>',title:'操作'}
        ]],
        url: app_root+"index?do=json",
        page: true,
        limit:<?php echo htmlentities($limit); ?>
    });/**/
    /*顶部添加按钮*/
    $('#top-zhusu-add').on('click',function(){Open();});/**/
    /*顶部删除按钮*/
    $('#top-zhusu-del').on('click', function(){
        var checkRows = table.checkStatus('zhusu').data;
        if(checkRows.length === 0){return layer.msg('请选择需删除的内容');}
        var ids = checkRows.map(function(d){return d.id;});
        del(ids);
    });/**/
    /*快编监听*/
    table.on('edit(zhusu)',function(obj){
        admin.req(app_root+"edit?do=up",{id:obj.data.id,av:obj.value,af:obj.field},function(res){
            layer.msg(res.msg,{shade:[0.4,'#000'],time:1500},function(){
                if(res.code==1) table.reloadData('zhusu');
            });
        },'post',{headersToken:true});
        
    });/**/
   
    /*工具条监听*/
    table.on('tool(zhusu)', function(obj){
        var data = obj.data;
        if(obj.event === 'edit'){
            Open(data);
        }else if(obj.event === 'del'){
            del(data.id);
        }
    });/**/

    /*删除*/
    function del(ids){
        layer.confirm('确定要删除所选内容吗？', function(){
            admin.req(app_root+"del",{id:ids},function(res){
                layer.msg(res.msg,{shade:[0.4,'#000'],time:1500},function(){
                    if(res.code==1) table.reloadData('zhusu');
                });
            },'post',{headersToken:true});
        });
    }/**/
    /*弹出窗*/
    function Open(Dt){
        admin.open({
            type: 1,
            bid: 'zhusu_items',
            btn: ['保存', '取消'],
            area: ['800px', '90%'],
            title: Dt ? '编辑' : '添加',
            success: function(l,index){
                var height = l.height() - 260;
                layui.buildItems.build({
                    bid: 'zhusu_items',
                    data: [
                        {name:"order_id",title:"订单号",type:"text",value:'',verify:'required',placeholder:"请输入订单号",must:true},
                        {name:"product_id",title:"产品名称",type:"html",html:'<div id="product-list-tree" class="v-xmselect-tree"></div>',must:true},
                        {name:"number",title:"机台编号",type:"html",html:'<div id="machine-number-list-tree" class="v-xmselect-tree"></div>',must:true},
                        {name:"material_id",title:"用料",type:"html",html:'<div id="material-list-tree" class="v-xmselect-tree"></div>',must:true},
                        {name:"colour_id",title:"颜色/色粉",type:"html",html:'<div id="colour-list-tree" class="v-xmselect-tree"></div>',must:true},
                        {name:"order_number",title:"订单数量",type:"text",value:'',verify:'required',placeholder:"请输入订单数量",must:true},
                        {name:"qualified_quantity",title:"合格数量",type:"text",value:'',verify:'required',placeholder:"请输入生产合格数量",must:true},
                        {name:"working_hours",title:"生产工时",type:"text",value:'',verify:'required',placeholder:"请输入实际生产工时",must:true},
                        {name:"scrapped_part",title:"残品数量",type:"text",value:'',verify:'required',placeholder:"请输入残品数量",must:true},
                        {name:"operator",title:"操作工",type:"html",html:'<div id="operator-list-tree" class="v-xmselect-tree"></div>',must:true},
                        {name:"weight",title:"产品单重",type:"text",value:'',verify:'required',placeholder:"请输入产品单重",must:true},
                        {name:"classes",title:"班次",type:"html",html:'<input type="radio" name="classes" value="1" title="早班" checked><input type="radio" name="classes" value="2" title="晚班" >'},                       
                        {name:"debugging_time",title:"换模调试",type:"text",value:'',placeholder:"请输入换模调试时间"},
                        {name:"change_material_time",title:"换料烘料",type:"text",value:'',placeholder:"请输入换料烘料时间"},
                        {name:"abnormal_time",title:"其他异常",type:"textarea",value:'',placeholder:"请输入其他异常时间"},
                        {name:"remarks",title:"备注",type:"textarea",value:'',placeholder:"备注"},
                        
                    ]
                });
                
                form.val('zhusu_items_form', Dt);
                var id = Dt ? Dt.id : 0;
                var loadIndex = layer.load(2);
                admin.req(app_root+"index?do=mjson",{id:id},function(res){
                    layer.close(loadIndex);
                    if(res.code === 0){
                     //   $.fn.zTree.init($('#zhusuTree'),{check:{enable:true},data:{simpleData:{enable:true}}},res.data);
                    }else{
                        layer.msg(res.msg,{icon:2});
                    }
                },'get');


     
                var organObj,organData,machine_number,material,colour,operator; /*左树 选中数据 和 总树数据*/
                function renderTree(data,load){

                    if(data){
                        organData = toTree(data);
                        machine_number= toTree(data);
                        material= toTree(data);
                        colour= toTree(data);
                    }else{
                       
                        admin.req(app_root + "index?do=organ",function(res){
                            organData = toTree(res);
                         
                        });
                    }
                }
         
                renderTree(<?=$product?>);
                /*渲染下拉树 https://maplemei.gitee.io/xm-select/#/component/options*/
                var product_data = JSON.parse(JSON.stringify(organData));
               
                //产品名称
                layui.xmSelect.render({
                    el: '#product-list-tree',
                    name: 'product_id',
                    tips: '选择产品',
                    height: '240px',
                    data: product_data,
                    filterable: true,
                    radio: true,
                    clickClose: true,
                    model: {label:{type:'text'}},
                    prop: {name:'name',value:'id',disabled:'disabled'},
                    initValue: [Dt ? Dt.product : 0],
                    tree: {show:true,indent:25,strict:false,expandedKeys:true}
                });

                renderTree(<?=$machine_number?>);
                var machine_number_data = JSON.parse(JSON.stringify(machine_number));
               //机台编号
                layui.xmSelect.render({
                    el: '#machine-number-list-tree',
                    name: 'number',
                    tips: '选择机台编号',
                    height: '240px',
                    data: machine_number_data,
                    filterable: true,
                    radio: true,
                    clickClose: true,
                    model: {label:{type:'text'}},
                    prop: {name:'name',value:'id',disabled:'disabled'},
                    initValue: [Dt ? Dt.number : 0],
                    tree: {show:true,indent:25,strict:false,expandedKeys:true}
                });

                renderTree(<?=$material?>);
                var material_data = JSON.parse(JSON.stringify(material));
         
               //用料
                layui.xmSelect.render({
                    el: '#material-list-tree',
                    name: 'material_id',
                    tips: '选择用料',
                    height: '240px',
                    data: material_data,
                    filterable: true,
                    radio: true,
                    clickClose: true,
                    model: {label:{type:'text'}},
                    prop: {name:'name',value:'id',disabled:'disabled'},
                    initValue: [Dt ? Dt.material : 0],
                    tree: {show:true,indent:25,strict:false,expandedKeys:true}
                });

                renderTree(<?=$colour?>);
                var colour_data = JSON.parse(JSON.stringify(colour));
              
               //颜色
                layui.xmSelect.render({
                    el: '#colour-list-tree',
                    name: 'colour_id',
                    tips: '选择颜色',
                    height: '240px',
                    data: colour_data,
                    filterable: true,
                    radio: true,
                    clickClose: true,
                    model: {label:{type:'text'}},
                    prop: {name:'name',value:'id',disabled:'disabled'},
                    initValue: [Dt ? Dt.colour : 0],
                    tree: {show:true,indent:25,strict:false,expandedKeys:true}
                });

            //操作工
                var operator_data = JSON.parse(JSON.stringify(<?=$operator?>));
                layui.xmSelect.render({
                    el: '#operator-list-tree',
                    name: 'operator',
                    tips: '选择操作工',
                    height: '240px',
                    data: operator_data,
                    filterable: true,
                    radio: true,
                    clickClose: true,
                    model: {label:{type:'text'}},
                    prop: {name:'truename',value:'userid',disabled:'disabled'},
                    initValue: [Dt ? Dt.operator_id : 0],
                    tree: {show:true,indent:25,strict:false,expandedKeys:true}
                });
                /*二维数组转为树形结构*/
                function toTree(data){
                    let result = []
                    let map = {};
                    if(!Array.isArray(data)){return result;}
                    data.forEach(item =>{
                        delete item.children;
                        item.spread = true;
                        map[item.id] = item;
                    });
                    data.forEach(item =>{
                        let parent = map[item.parentid];
                        if(parent){
                            (parent.children || (parent.children = [])).push(item);
                        } else {
                            result.push(item);
                        }
                    });
                    return result;
                }

                form.on('submit(zhusu_items)',function(data){
                    var btn = $(this);
                    if (btn.attr('stop')){return false}else{btn.attr('stop',1)}
                    var field = data.field; field.id = id;
                    var post_url = field.id ? app_root+'edit' : app_root+'add';

                    admin.req(post_url,field,function(res){
                        layer.msg(res.msg,{shade:[0.4,'#000'],time:1500},function(){
                            console.log(res);
                            if(res.code==1){
                                layer.close(index);
                                table.reloadData('zhusu');
                            }
                            btn.removeAttr('stop');
                        });
                    },'post',{headersToken:true});
                    return false;
                });

                
            }
        });
    }/**/
});
</script>